﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net;

namespace Hourglass.Qrobot
{
    public sealed class QQContext : IDisposable
    {
        private QQState _state;

        internal RuleConfig Config { get; set; }

        public IDisplayer Displayer { get; set; }

        public SmartQQ Qq { get; set; }

        public CookieContainer CookieHolder { get; set; }

        public LoginedVerfiedField VerfiedField { get; set; }

        public Exception Exception { get; set; }

        public bool HasException => Exception != null;

        public QQState State
        {
            get { return this._state; }
            set { this._state = value; }
        }

        public QQContext(QQState state)
        {
            this._state = state;
            this.Config = RuleConfig.GetConfig();
            this.CookieHolder = new CookieContainer();
            this.VerfiedField = new LoginedVerfiedField(CookieHolder);
        }

        public void Process()
        {
            _state.Handler(this);
        }

        public void Dispose()
        {
        }

        public class LoginedVerfiedField
        {
            private readonly CookieContainer _cookieContainer;

            internal LoginedVerfiedField(CookieContainer cookieContainer)
            {
                this._cookieContainer = cookieContainer;
            }

            public LoginedVerfiedField() { }

            public string Vfwebqq { get; set; }

            public string Ptwebqq
            {
                get { return _cookieContainer?.GetCookies(new Uri(QrobotConsts.DOMAN_Vfwebqq))["ptwebqq"]?.Value; }
                set { _cookieContainer?.Add(new Cookie("ptwebqq", value, "/", QrobotConsts.DOMAN_Vfwebqq));}
            } 

            public string Psessionid { get; set; }

            public long Qq { get; set; }

            public string Qhash { get; set; }
        }
    }
}
